
import Web3 from "web3";
import config from "@/data/config.json"

const configs = config.chian1

const getWeb3 = () => {
  let network = configs.url
  if (!window.web3js) {
    if (typeof web3js !== 'undefined') {
      window.web3js = new Web3(window.web3.currentProvider);
      return new Web3(window.web3.currentProvider);
    } else {
      window.web3js = new Web3(new Web3.providers.HttpProvider(network));
      return new Web3(new Web3.providers.HttpProvider(network));
    }
  }
}

const isContract = async (str) => {
  if (window.web3js) {
    let code = await window.web3js.eth.getCode(str)
    if (code == '0x') {
      return false
    } else {
      return true;
    }
  }
  // return false
}

const isAddress = (str) => {
  if (window.web3js && str) {
    return window.web3js.utils.isAddress(str);
  }
  return false
}

export {
  getWeb3,
  isContract,
  isAddress
};